<style lang="less">
    @import "debitBatchManagement.less";
</style>
<template>
    <div>
        <Card>
            <Row>
                <Form  ref="searchForm" :model="searchForm" inline :label-width="90" class="search-form">
                    <div class="search">
                        <Row>
                            <Col align="left">
                                <Form-item label="批次号码" prop="batchNo">
                                    <Input type="text" v-model="searchForm.batchNo" clearable placeholder="请输入批次号码"
                                           />
                                </Form-item>
                                <Form-item label="扣款银行" prop="deductBank">
                                    <Select v-model="searchForm.deductBank"  clearable placeholder="请选择扣款银行">
                                        <Option v-for="item in dataDic.deductBank" :value="item.value" :label="item.title" :key="item.value">{{ item.title }}</Option>
                                    </Select>
                                </Form-item>
                                <Form-item label="批次状态" prop="batchStatus">
                                    <Select v-model="searchForm.batchStatus"  clearable placeholder="请选择批次状态">
                                        <Option v-for="item in dataDic.batchStatus" :value="item.value" :label="item.title" :key="item.value">{{ item.title }}</Option>
                                    </Select>
                                </Form-item>
                                <Form-item label="生成日期">
                                    <DatePicker v-model="selectDate" type="datetimerange" format="yyyy-MM-dd HH:mm:ss" clearable
                                                @on-change="selectDateRange" placeholder="选择起始时间">
                                    </DatePicker>
                                </Form-item>
                            </Col>
                        </Row>
                    </div>
                    <Row>
                        <Col align="middle">
                            <Form-item>
                                <Button @click="handleSearch" type="primary" icon="ios-search">搜索</Button>
                                <Button @click="handleReset" type="primary" icon="ios-trash">重置</Button>
                            </Form-item>
                        </Col>
                    </Row>
                    <Row>
                        <Col>
                            <Form-item label="扣款银行" prop="generateBank">
                                <Select v-model="searchForm.generateBank" style="width:200px" clearable placeholder="请选择扣款银行">
                                    <Option value="" label="全部">全部</Option>
                                    <Option v-for="item in dataDic.deductBank" :value="item.value" :label="item.title" :key="item.value">{{ item.title }}</Option>
                                </Select>
                                <Button @click="generateBatch" type="primary" icon="ios-build-outline">生成扣款批次</Button>
                                <Button @click="getDebitBatch" type="primary" icon="ios-refresh">刷新</Button>
                            </Form-item>
                        </Col>
                    </Row>
                    <Row>
                        <Table :loading="loading" border :columns="columns" :data="data" sortable="custom"
                               @on-sort-change="changeSort" @on-selection-change="showSelect" ref="table">
                            <template slot-scope="{ row, index }" slot="id">
                                <Button v-show="row.batchStatus == 'WAIT_SEND' || row.batchStatus == 'FAIL_SEND'"
                                        type="primary" size="small" @click="sendBatch(row)">发送</Button>
                                <Button v-show="row.batchStatus == 'WAIT_SEND' || row.batchStatus == 'FAIL_SEND'"
                                        type="error" size="small" @click="deleteBatch(row)">删除</Button>
                                <Button v-show="(row.batchStatus == 'SUCCESS_SEND' || row.batchStatus == 'FAIL_FEEDBACK' || row.batchStatus == 'SUCCESS_FEEDBACK')
                                            && row.goingCount>0"
                                        type="error" size="small" @click="queryBatch(row)">回盘</Button>
                            </template>
                            <template slot-scope="{ row, index }" slot="batchNo">
                                <span><a @click="showBatchDetail(row)">{{row.batchNo}}</a></span>
                            </template>
                            <template slot-scope="{ row, index }" slot="batchStatus">
                                <span>{{setDictTitleByType("batchStatus",row.batchStatus)}}</span>
                            </template>
                            <template slot-scope="{ row, index }" slot="deductBank">
                                <span>{{setDictTitleByType("deductBank",row.deductBank)}}</span>
                            </template>

                        </Table>
                    </Row>
                    <Row type="flex" justify="end" class="page">
                        <Page :current="searchForm.pageNumber" :total="total" :page-size="searchForm.pageSize"
                              @on-change="changePage" @on-page-size-change="changePageSize" :page-size-opts="[10,20,50]"
                              size="small" show-total show-elevator show-sizer></Page>
                    </Row>
                </Form>
            </Row>
        </Card>
    </div>
</template>

<script>

    import {
        getByTypes
    } from "_p/basic/api/admin/datadic";

    import {
        queryDebitBatchList,
        deleteDebitBatch,
        generateDebitBatch,
        sendDebitBatch,
        queryDebitBatchResult
    } from "@/projects/basic/api/contract/debit-management/debitManagement";


    export default {
        name: "debit-batch-management",
        comments:{
        },
        data() {
            return {
                searchForm:{batchNo:"",startDate:"",endDate:"",deductBank:"",batchStatus:"",generateBank:"",},
                data:[],
                total: 0,
                selectDate: null,
                dicKeys: [
                    "deductBank","batchStatus"
                ],
                dataDic:{},
                loading: false,
                columns: [
                    {title: "操作",key: "id",minWidth: 150,align: "center",slot: "id",fixed:"left"},
                    {title: "批次号码",key: "batchNo",minWidth: 160,align: "center",slot:"batchNo"},
                    {title: "生成时间",key: "createTime",minWidth: 150,align: "center"},
                    {title: "批次状态",key: "batchStatus",minWidth: 150,align: "center",slot: "batchStatus"},
                    {title: "总记录数",key: "count",minWidth: 150,align: "center"},
                    {title: "总金额",key: "amount",minWidth: 150,align: "center"},
                    {title: "成功记录数",key: "successCount",minWidth: 150,align: "center"},
                    {title: "扣款银行",key: "deductBank",minWidth: 150,align: "center",slot: "deductBank"}
                ]
            }
        },
        methods:{
            sendBatch(v){
                let params = {
                    id:v.id
                }
                this.loading = true;
                sendDebitBatch(params).then(res => {
                    if (res.code === "0000") {
                        this.$Message.success(res.data);
                        this.getDebitBatch();
                    }
                }).catch(() => {
                    this.loading = false;
                });
            },
            showBatchDetail(v){
                /**
                 *
                 */
                this.afs.newTab(this,
                    'projects/basic/pages/contract/debit-management/batch/debitBatchDetail',
                    '扣款批次详情，批次号' + v.batchNo,
                    'ios-list',
                    {batchId:v.id},
                    "debit-batch-detail"+v.batchNo,
                    [],
                    true)
            },
            deleteBatch(v){
                let params = {
                    batchNo:v.batchNo,
                    id:v.id
                }
                this.loading = true;
                deleteDebitBatch(params).then(res => {
                    if (res.code === "0000") {
                        this.$Message.success(res.data);
                        this.getDebitBatch();
                    }
                }).catch(() => {
                    this.loading = false;
                });
            },
            generateBatch(v){
                this.loading = true;
                generateDebitBatch(this.searchForm).then(res => {
                    if (res.code === "0000") {
                        this.$Message.success(res.data);
                        this.getDebitBatch();
                    }
                });
            },
            queryBatch(v){
                let params = {
                    id:v.id
                }
                this.loading = true;
                queryDebitBatchResult(params).then(res => {
                    if (res.code === "0000") {
                        this.$Message.success(res.data);
                        this.getDebitBatch();
                    }
                }).catch(() => {
                    this.loading = false;
                });;
            },
            handleSearch() {
                this.searchForm.pageNumber = 1;
                this.searchForm.pageSize = 10;
                this.getDebitBatch();
            },
            handleReset() {
                this.$refs.searchForm.resetFields();
                this.searchForm.pageNumber = 1;
                this.searchForm.pageSize = 10;
                this.selectDate = null;
                this.searchForm.startDate = "";
                this.searchForm.endDate = "";

                // 重新加载数据
                this.getDebitBatch();
            },
            selectDateRange(v) {
                if (v) {
                    this.searchForm.startDate = v[0];
                    this.searchForm.endDate = v[1];
                }
            },
            changePage(v) {
                this.searchForm.pageNumber = v;
                this.getDebitBatch();
            },
            changePageSize(v) {
                this.searchForm.pageSize = v;
                this.getDebitBatch();
            },
            initDataDic(){
                let self = this;
                getByTypes(this.dicKeys).then(res => {
                    if (res.code === "0000" && res.data) {
                        self.dataDic = res.data;
                    }
                });
            },
            getDic(v){
                let dic = []
                switch(v) {
                    case "deductBank":
                        dic = this.dataDic.deductBank;
                        break;
                    case "batchStatus":
                        dic = this.dataDic.batchStatus;
                        break;
                    default:
                }
                return dic;
            },
            setDictTitleByType(v1,v2){
                let dic = {}
                if(!this.getDic(v1)) return '';
                this.getDic(v1).forEach(column => {
                    if(column.value === v2){
                        dic = column;
                    }
                });
                return dic.title;
            },
            changeSort(e) {
                this.searchForm.sort = e.key;
                this.searchForm.order = e.order;
                if (e.order === "normal") {
                    this.searchForm.order = "";
                }
                // this.getTaskList();
            },
            showSelect(e) {
                this.selectList = e;
                this.selectCount = e.length;
            },
            getDebitBatch() {
                // 多条件搜索任务列表
                this.loading = true;
                queryDebitBatchList(this.searchForm).then(res => {
                    this.loading = false;
                    if (res.code === "0000") {
                        this.data = res.data.records;
                        this.total = res.data.total;
                    }
                });
                this.selectCount=0;
            },
            init(){
                this.searchForm.pageNumber = 1;
                this.searchForm.pageSize = 10;
                this.getDebitBatch();
            },
        },
        mounted() {
            this.initDataDic();
            this.init();
        }
    }
</script>

<style scoped>

</style>
